package com.darknight.ftp.connection;

import com.darknight.ftp.util.StringUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.util.Assert;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by DarKnight on 2017/5/26.
 */
public class FtpUserConnection {
    private String ftpHostName;

    private Integer ftpPort;

    private final FTPClient ftpClient = new FTPClient();

    public FtpUserConnection() {
    }

    public FtpUserConnection(String ftpHostName, Integer ftpPort) {
        this.ftpHostName = ftpHostName;
        this.ftpPort = ftpPort;
    }

    public String getFtpHostName() {
        return ftpHostName;
    }

    public void setFtpHostName(String ftpHostName) {
        this.ftpHostName = ftpHostName;
    }

    public Integer getFtpPort() {
        return ftpPort;
    }

    public void setFtpPort(Integer ftpPort) {
        this.ftpPort = ftpPort;
    }

    /**
     * 通过FTP客户端连接FTP服务器
     * @throws IOException
     */
    public void connectFtpServer() throws IOException {
        Assert.notNull(ftpHostName, "FTP访问路径不能为空");
        Assert.notNull(ftpPort, "FTP访问端口不能为空");

        // 连接FTP服务器
        ftpClient.connect(ftpHostName, ftpPort);
    }

    /**
     * 通过FTP客户端登录FTP服务器
     * @param account
     * @param password
     * @throws IOException
     */
    public void loginFtpServer(String account, String password) throws IOException {
        Assert.notNull(account, "FTP登录账号不能为空");

        // 用户登录
        ftpClient.login(account, password);

        System.out.println("FTP服务器登录账户为：" + account);
        System.out.println("FTP服务器登录密码为：" + password);
    }

    /**
     * 判断FTP客户端是否登录成功
     * @return
     * @throws IOException
     */
    public boolean isLoginSuccess() throws IOException {
        int reply = ftpClient.getReplyCode();

        if(FTPReply.isPositiveCompletion(reply)) {

            System.out.println("FTP服务器登录成功");
            return true;
        }

        System.out.println("FTP服务器登录失败");
        return false;
    }

    public boolean logout() throws IOException {
        return ftpClient.logout();
    }

    public void disconnect() throws IOException {
        if (ftpClient.isConnected()) {
            ftpClient.disconnect();
        }
    }

//    /**
//     * 传入的文件存储路径是否存在
//     * @param storeFilePath 文件存储路径
//     * @return
//     */
//    private boolean hasStoreFilePath(String storeFilePath) {
//        return StringUtils.isNotBlank(storeFilePath);
//    }

    /**
     * 判断切换到传入的文件存储路径下是否成功
     * 将文件存储路径分割为若干父子级目录，并依次判断目录是否存在
     * 若目录存在则进入该目录，否则创建指定目录并进入
     * @param storeFilePath 文件存储路径
     * @return
     * @throws IOException
     */
    public boolean reachStoreFilePath(String storeFilePath) throws IOException {
        boolean reachStoreFilePathResult = true;
        String[] directoriesAry = StringUtils.split(storeFilePath, "/");

        // 初次切换目录时以根 homedirectory 为起点
        // 之后每次切换都以前一次切换后的目录为新起点
        for(String dir : directoriesAry) {
            reachStoreFilePathResult = ftpClient.changeWorkingDirectory(dir);
            if(!reachStoreFilePathResult) {
                ftpClient.makeDirectory(dir);
                reachStoreFilePathResult = ftpClient.changeWorkingDirectory(dir);
            }
        }

        return reachStoreFilePathResult;
    }

    public boolean storeFile(String storeFileName, InputStream inputStream) throws IOException {
        Assert.hasText(storeFileName, "待上传文件名为空");
        Assert.notNull(inputStream, "上传文件输入流为空");

        ftpClient.setControlEncoding("UTF-8");
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        boolean optSuccess = ftpClient.storeFile(storeFileName, inputStream);

        return optSuccess;
    }
}
